package org.youchan.aco.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

import org.youchan.aco.p2p.A2AManager;

import com.skype.SkypeException;

public class MainWindow extends JFrame {
	private static final long serialVersionUID = 950742861557963003L;
	
	private static final String TAB_MEMBERS_LIST = UIManager.getResource("tab.members_list");
	private static final String TAB_TIMELINE = UIManager.getResource("tab.timeline");
	
	private JTabbedPane mTabbedPane;
	private MembersViewPanel mMembersViewPanel;
	private TimelineViewPanel mTimelineViewPanel;
	private JLabel mStatusLabel;
	
	public MainWindow() {
		super();
		init();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Thread() {
					public void run() {
						try {
							A2AManager.getInstance().connectToAll();
						} catch (SkypeException e) {
							e.printStackTrace();
						}
					}
				}.start();
			}
		});
	}
	
	private void init() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setJMenuBar(new MainMenu());
		
		mTabbedPane = new JTabbedPane();
		this.getContentPane().add(mTabbedPane, BorderLayout.CENTER);

		mMembersViewPanel = new MembersViewPanel();
		mTabbedPane.add(TAB_MEMBERS_LIST, mMembersViewPanel);
		
		mTimelineViewPanel = new TimelineViewPanel();
		mTabbedPane.add(TAB_TIMELINE, mTimelineViewPanel);
		
		mStatusLabel = new JLabel();
		mStatusLabel.setMinimumSize(new Dimension(128, 24));
		mStatusLabel.setPreferredSize(new Dimension(128, 24));
		mStatusLabel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		Font font = mStatusLabel.getFont();
		mStatusLabel.setFont(font.deriveFont(Font.PLAIN));
		this.getContentPane().add(mStatusLabel, BorderLayout.SOUTH);
		
		this.pack();
	}
	
	public void updateStatusLine(final String text) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				mStatusLabel.setForeground(Color.black);
				mStatusLabel.setText(text);
			}
			
		});
	}

	public void updateStatusLine(final String text, final Color color) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				mStatusLabel.setForeground(color);
				mStatusLabel.setText(text);
			}
			
		});
	}
}
